#===============================================================================
# Koro Business Environment
# Copyright (C) 2006  Antonio Corroppoli
#
#This file is part of Koro BE.
# 
# Koro BE is free software; you can redistribute it and/or
# modify it under the terms of the GNU General Public License
# as published by the Free Software Foundation; either version 2
# of the License, or (at your option) any later version.
# 
# Koro BE is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
# GNU General Public License for more details.
# 
# You should have received a copy of the GNU General Public License
# along with Koro BE; if not, write to the Free Software
# Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, USA.
#===============================================================================

#-----------------------------------------------------------------------------
# Name:        UAD.py
# Purpose:     User Autentication Dialog
#
# Author:      <your name>
#
# Created:     2006/12/04
# RCS-ID:      $Id: UAD.py $
# Copyright:   (c) 2004
# Licence:     <your licence>
# New field:   Whatever
#-----------------------------------------------------------------------------

import sys
import sha
import wx
import images
import controls as co
from mellcfg import *


def _(s):
    return s



def create(parent):
    return AuthDialog(parent)

[mID_AUTHDIALOG, mID_UAD_EXIT, mID_UAD_LOGIN, 
 mID_UAD_PASS,mID_UAD_USR, 
] = [wx.NewId() for _init_ctrls in range(5)]

class AuthDialog(wx.Dialog):
    def __init__(self, prnt):
        wx.Dialog.__init__(self, id=mID_AUTHDIALOG, name='AuthDialog',
              parent=prnt, style=wx.NO_BORDER | wx.THICK_FRAME, title='Auth Dialog')
        #self.SetBestFittingSize(wx.Size(294, 195))
        self.SetAutoLayout(True)
        self.Center(wx.BOTH)
        self.__data = None
        self._do_layout()
        self.__bindButton()
        
    def _do_layout(self):
        
        #lborder = 10
        tborder = 0

        box = wx.StaticBox(self, -1, _("Connect to GAP"))
        bsizer = wx.StaticBoxSizer(box, wx.VERTICAL)

        topsizer = wx.BoxSizer(wx.VERTICAL)

        #sizer = wx.BoxSizer(wx.VERTICAL)
##        BORDER = wx.TOP|wx.LEFT|wx.RIGHT
        

        #lbsizer= wx.BoxSizer(wx.VERTICAL)
        #txtsizer = wx.BoxSizer(wx.VERTICAL)

        #logo1 = images.getLogo1Bitmap()
        logo2 = images.getLogo2Bitmap()
        logo=wx.StaticBitmap(self, -1, logo2, size=(logo2.GetWidth(), logo2.GetHeight()))
        topsizer.Add(logo,0,wx.SHAPED|wx.ALL,0)

        #lb = wx.StaticText(self,wxID_AUTHDIALOGLB_USER, _('User:'))
        self.userTxt = co.mTextCtrl(id=mID_UAD_USR,parent=self,size=wx.Size(228, -1),maxlen=30)
        self.userTxt.SetFocus()
        #self.userTxt.SetMaxLength(30)
        #lbsizer.Add(lb,0,BORDER|wx.ALIGN_CENTER_VERTICAL|wx.ALIGN_RIGHT,lborder)
        bsizer.Add(co.CompositeCtrl(self.userTxt,label=_('User')),0,wx.ALL,mDefaultBorder)
##        bsizer.Add(self.userTxt,0,wx.ALL,mDefaultBorder)
        #self.userTxt.SetFocus()
        #sizer = wx.BoxSizer(wx.VERTICAL)
        #lb = wx.StaticText(self,wxID_AUTHDIALOGLB_PASS,_('Password:'))
        self.passTxt = co.mTextCtrl(id=mID_UAD_PASS,parent=self, size=wx.Size(228, -1), style=wx.TE_PASSWORD,maxlen=30)
        #self.passTxt.SetMaxLength(30)
        #lbsizer.Add(lb,0,BORDER|wx.ALIGN_CENTER_VERTICAL,lborder)
        bsizer.Add(co.CompositeCtrl(self.passTxt,label=_('Password')),0,wx.ALL,mDefaultBorder)

        butsizer = wx.BoxSizer(wx.HORIZONTAL)
        self.loginBut = wx.Button(id=mID_UAD_LOGIN, label=_('Login'), parent=self,size=wx.Size(75, 30))
        self.exitBut = wx.Button(id=mID_UAD_EXIT, label=_('Exit'), parent=self, size=wx.Size(75, 30))
        butsizer.Add(self.loginBut,0,wx.BOTTOM,6)
        butsizer.Add((80,30),2,wx.EXPAND|wx.ALL|wx.GROW,0)
        butsizer.Add(self.exitBut,0,wx.BOTTOM,6)        

        #bsizer.Add(lbsizer,0)
        #bsizer.Add(txtsizer,0)
        
        
        topsizer.Add(bsizer,0,wx.EXPAND|wx.TOP|wx.LEFT|wx.RIGHT,20)
        topsizer.Add(butsizer,0,wx.EXPAND|wx.TOP|wx.LEFT|wx.RIGHT,20)

        self.SetSizer(topsizer)
        self.Fit()        
        self.Layout()
        #self.userTxt.SetFocus()

    def __bindButton(self):
        self.Bind(wx.EVT_BUTTON,self.__esci,id=mID_UAD_EXIT)
        self.Bind(wx.EVT_BUTTON,self.__login,id=mID_UAD_LOGIN)
        
    def __esci(self,event):
        self.EndModal(-1)

    def __login(self,event):

        connDict = {"Host" : "localhost", "User" : self.userTxt.GetValue(),
                    "Password" : sha.new(self.passTxt.GetValue()).hexdigest()}
        
##        self.__data = (self.txt_user.GetValue(),#self.txt_pass.GetValue())
##                       sha.new( self.txt_pass.GetValue() ).hexdigest(),
##                       "localhost","test")
        self.__data = connDict
        self.EndModal(0)
        
    def getData(self):
        d=self.__data
        return d

    def setData(self,data):
        if isinstance(data,dict):
            self.userTxt.SetValue(data['User'])
            self.passTxt.SetValue('')
            self.userTxt.SetFocus()
            self.__data['User']=data['User']
            self.__data['Password']=''

if __name__ == '__main__':
    app = wx.PySimpleApp()
    d=AuthDialog(None)
    print d.ShowModal()
    d.Destroy()
